copier.js ➔ copyOrders   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 61
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.2742

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 61
ccs 7
cts 9
cp 0.7778
rs 8.1696
c 0
b 0
f 0
cc 5
crap 5.2742

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1 1
const db = require("../db/database.js");
2 1
const products = require("./products.js");
3 1
const orders = require("./orders.js");
4
5
let config;
6
7 1
try {
8 1
    config = require('../../config/config.json');
9
} catch (error) {
10 1
    console.error(error);
11
}
12
13 1
const copier = (function () {
14 2
    const copyApiKey = process.env.COPY_API_KEY || config.copyApiKey;
15
16
    function copyAll(res, apiKey) {
17 1
        let sql = "INSERT INTO products" +
18
            " (productId," +
19
            " articleNumber," +
20
            " productName," +
21
            " productDescription," +
22
            " productSpecifiers," +
23
            " stock," +
24
            " location," +
25
            " price," +
26
            " apiKey)" +
27
            " SELECT productId," +
28
            " articleNumber," +
29
            " productName," +
30
            " productDescription," +
31
            " productSpecifiers," +
32
            " stock," +
33
            " location, " +
34
            " price," +
35
            "'" + apiKey + "'" +
36
            " FROM products" +
37
            " WHERE apiKey = ?";
38
39 1
        db.run(sql, copyApiKey, (err) => {
40 2
            if (err) {
41
                return res.status(500).json({
42
                    errors: {
43
                        status: 500,
44
                        source: "/copy_products",
45
                        title: "Database error",
46
                        detail: err.message
47
                    }
48
                });
49
            } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
50 1
                let sql = "INSERT INTO orders" +
51
                    " (orderId," +
52
                    " customerName," +
53
                    " customerAddress," +
54
                    " customerZip," +
55
                    " customerCity," +
56
                    " customerCountry," +
57
                    " statusId," +
58
                    " apiKey)" +
59
                    " SELECT orderId," +
60
                    " customerName," +
61
                    " customerAddress," +
62
                    " customerZip," +
63
                    " customerCity," +
64
                    " customerCountry," +
65
                    " statusId," +
66
                    "'" + apiKey + "'" +
67
                    " FROM orders" +
68
                    " WHERE apiKey = ?";
69
70 1
                db.run(sql, copyApiKey, (err) => {
71 2
                    if (err) {
72
                        return res.status(500).json({
73
                            errors: {
74
                                status: 500,
75
                                source: "/copy_orders",
76
                                title: "Database error",
77
                                detail: err.message
78
                            }
79
                        });
80
                    } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
81 1
                        let orderItemsSQL = "INSERT INTO order_items" +
82
                            " (orderId," +
83
                            " productId," +
84
                            " amount," +
85
                            " apiKey)" +
86
                            " SELECT orderId," +
87
                            " productId," +
88
                            " amount," +
89
                            "'" + apiKey + "'" +
90
                            " FROM order_items" +
91
                            " WHERE apiKey = ?";
92
93 1
                        db.run(orderItemsSQL, copyApiKey, (err) => {
94 2
                            if (err) {
95
                                return res.status(500).json({
96
                                    errors: {
97
                                        status: 500,
98
                                        source: "/copy_orders",
99
                                        title: "Database error in order_items",
100
                                        detail: err.message
101
                                    }
102
                                });
103
                            } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
104 1
                                return res.status(201).json({
105
                                    data: {
106
                                        message: "Products and orders have been copied"
107
                                    }
108
                                });
109
                            }
110
                        });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
111
                    }
112
                });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
113
            }
114
        });
115
    }
116
117
    function copyProducts(res, apiKey) {
118 1
        let sql = "INSERT INTO products" +
119
            " (productId," +
120
            " articleNumber," +
121
            " productName," +
122
            " productDescription," +
123
            " productSpecifiers," +
124
            " stock," +
125
            " location," +
126
            " price," +
127
            " apiKey)" +
128
            " SELECT productId," +
129
            " articleNumber," +
130
            " productName," +
131
            " productDescription," +
132
            " productSpecifiers," +
133
            " stock," +
134
            " location, " +
135
            " price," +
136
            "'" + apiKey + "'" +
137
            " FROM products" +
138
            " WHERE apiKey = ?";
139
140 1
        db.run(sql, copyApiKey, (err) => {
141 2
            if (err) {
142
                return res.status(500).json({
143
                    errors: {
144
                        status: 500,
145
                        source: "/copy_products",
146
                        title: "Database error",
147
                        detail: err.message
148
                    }
149
                });
150
            } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
151 1
                products.getAllProducts(res, apiKey, 201);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
152
            }
153
        });
154
    }
155
156
    function copyOrders(res, apiKey) {
157 1
        let sql = "INSERT INTO orders" +
158
            " (orderId," +
159
            " customerName," +
160
            " customerAddress," +
161
            " customerZip," +
162
            " customerCity," +
163
            " customerCountry," +
164
            " statusId," +
165
            " apiKey)" +
166
            " SELECT orderId," +
167
            " customerName," +
168
            " customerAddress," +
169
            " customerZip," +
170
            " customerCity," +
171
            " customerCountry," +
172
            " statusId," +
173
            "'" + apiKey + "'" +
174
            " FROM orders" +
175
            " WHERE apiKey = ?";
176
177 1
        db.run(sql, copyApiKey, (err) => {
178 2
            if (err) {
179
                return res.status(500).json({
180
                    errors: {
181
                        status: 500,
182
                        source: "/copy_orders",
183
                        title: "Database error",
184
                        detail: err.message
185
                    }
186
                });
187
            } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
188 1
                let orderItemsSQL = "INSERT INTO order_items" +
189
                    " (orderId," +
190
                    " productId," +
191
                    " amount," +
192
                    " apiKey)" +
193
                    " SELECT orderId," +
194
                    " productId," +
195
                    " amount," +
196
                    "'" + apiKey + "'" +
197
                    " FROM order_items" +
198
                    " WHERE apiKey = ?";
199
200 1
                db.run(orderItemsSQL, copyApiKey, (err) => {
201 2
                    if (err) {
202
                        return res.status(500).json({
203
                            errors: {
204
                                status: 500,
205
                                source: "/copy_orders",
206
                                title: "Database error in order_items",
207
                                detail: err.message
208
                            }
209
                        });
210
                    } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
211 1
                        orders.getAllOrders(res, apiKey, 201);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
212
                    }
213
                });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
214
            }
215
        });
216
    }
217
218 1
    return {
219
        copyAll: copyAll,
220
        copyProducts: copyProducts,
221
        copyOrders: copyOrders,
222
    };
223
}());
224
225
module.exports = copier;
226